/** Description of AbstractDbAdapter.java 
 *
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.database;

import java.text.SimpleDateFormat;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public abstract class AbstractDbAdapter {
	
	/** Smoke App Database Name */
	private static final String DB_NAME = "SmokersDB";
	
	/** Smoke App Database Version */
	private static final int DB_VERSION = 8;
	
	/** Smoke App Table Names */
	protected static final String TIMES_TRACK = "TimesTrack";
	protected static final String DAYS_TRACK = "DaysTrack";
	protected static final String WEEKS_TRACK = "WeeksTrack";
	protected static final String MONTHS_TRACK = "MonthsTrack";
	protected static final String BRANDS_TRACK = "BrandsTrack";
	
	/** Smoke App TIME_TRACK Column Names */
	public static final String TT_PK_ID = "_id";
	public static final String TT_DATETIME = "DateTime";
	public static final String TT_BRAND_ID = "BrandId";
	public static final String TT_COST = "Cost";
	
	/** Smoke App DAYS_TRACK Column Names */
	public static final String DT_PK_ID = "_id";
	public static final String DT_STARTDATE = "StartDate";
	public static final String DT_ENDDATE = "EndDate";
	public static final String DT_NO_OF_CIGGS = "NumberOfCigarettes";
	public static final String DT_COST = "Cost";
	
	/** Smoke App WEEKS_TRACK Column Names */
	public static final String WT_PK_ID = "_id";
	public static final String WT_STARTDATE = "StartDate";
	public static final String WT_ENDDATE = "EndDate";
	public static final String WT_NO_OF_CIGGS = "NumberOfCigarettes";
	public static final String WT_COST = "Cost";
	
	/** Smoke App MONTHS_TRACK Column Names */
	public static final String MT_PK_ID = "_id";
	public static final String MT_STARTDATE = "StartDate";
	public static final String MT_ENDDATE = "EndDate";
	public static final String MT_NO_OF_CIGGS = "NumberOfCigarettes";
	public static final String MT_COST = "Cost";
	
	/** Smoke App BRANDS_TRACK Column Names */
	public static final String BT_PK_ID = "_id";
	public static final String BT_IMAGE_URI = "ImageURI";
	public static final String BT_BRAND = "Brand";
	public static final String BT_NO_OF_CIGGS = "NumberOfCigarettes";
	public static final String BT_BRAND_PRICE = "BrandPrice";
	
	/** Smoke App Tables Create */
	private static final String TT_CREATE = "create table "+TIMES_TRACK+" ("+DT_PK_ID+" integer primary key autoincrement, "+TT_DATETIME+" date not null, "+TT_BRAND_ID+" integer not null, "+TT_COST+" real not null);";
	private static final String DT_CREATE = "create table "+DAYS_TRACK+" ("+DT_PK_ID+" integer primary key autoincrement, "+DT_STARTDATE+" date not null, "+DT_ENDDATE+" date not null, "+DT_NO_OF_CIGGS+" integer not null, "+DT_COST+" real not null);";
	private static final String WT_CREATE = "create table "+WEEKS_TRACK+"("+WT_PK_ID+" integer primary key autoincrement, "+WT_STARTDATE+" date not null, "+WT_ENDDATE+" date not null, "+WT_NO_OF_CIGGS+" integer not null, "+WT_COST+" real not null);";
	private static final String MT_CREATE = "create table "+MONTHS_TRACK+"("+MT_PK_ID+" integer primary key autoincrement, "+MT_STARTDATE+" date not null, "+MT_ENDDATE+" date not null, "+MT_NO_OF_CIGGS+" integer not null, "+MT_COST+" real not null);";
	private static final String BT_CREATE = "create table "+BRANDS_TRACK+"("+BT_PK_ID+" integer primary key autoincrement, "+BT_IMAGE_URI+" text not null, "+BT_BRAND+" text not null, "+BT_NO_OF_CIGGS+" integer not null, "+BT_BRAND_PRICE+" real not null);";
	
	/** SmokersDbAdapter TAG */
	private static final String TAG = "SmokersDbAdapter";
	
	private DatabaseHelper mDbHelper;
	protected SQLiteDatabase mDb;
	private final Context mCtx;
	
	/** Date time format that should be use to save datetime in date type column */
	public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context ctx) {
			super(ctx, DB_NAME, null, DB_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(TT_CREATE);
			db.execSQL(DT_CREATE);
			db.execSQL(WT_CREATE);
			db.execSQL(MT_CREATE);
			db.execSQL(BT_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");
			db.execSQL("drop table if exists "+TIMES_TRACK);
			db.execSQL("drop table if exists "+DAYS_TRACK);
			db.execSQL("drop table if exists "+WEEKS_TRACK);
			db.execSQL("drop table if exists "+MONTHS_TRACK);
			db.execSQL("drop table if exists "+BRANDS_TRACK);
			onCreate(db);
		}
		
	}
	
	public AbstractDbAdapter(Context context) {
		this.mCtx = context;
	}
	
	public AbstractDbAdapter open() {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}
	
	public void close() {
		mDbHelper.close();
	}
	
	/** Must be overridden create method */
	public abstract long create(ContentValues values);
	/** Must be overridden update method */
	public abstract long update(long _id, ContentValues values);
	/** Must be overridden delete method */
	public abstract long delete(long _id);
	/** Must be overridden fetch all method */
	public abstract Cursor fetchAll();
	/** Must be overridden fetch particular method */
	public abstract Cursor fetch(long _id);
	/** Must be overridden fetch oldest method */
	public abstract Cursor fetchOldest();
	/** Must be overridden fetch latest method */
	public abstract Cursor fetchLatest();
}
